package me.jessyan.mvparms.demo.utils;


import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;


import java.util.ArrayList;

import me.jessyan.mvparms.demo.R;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * 图片选择器
 *
 */
public class MyMultiImageSelector {

    public static final String EXTRA_RESULT = MultiImageSelectorActivity.EXTRA_RESULT;

    private boolean mShowCamera = true;
    private int mMaxCount = 9;
    private int mMode = MultiImageSelectorActivity.MODE_MULTI;
    private ArrayList<String> mOriginData;
    private static MyMultiImageSelector sSelector;

    @Deprecated
    private MyMultiImageSelector(Context context){

    }

    private MyMultiImageSelector(){}

    @Deprecated
    public static MyMultiImageSelector create(Context context){
        if(sSelector == null){
            sSelector = new MyMultiImageSelector(context);
        }
        return sSelector;
    }

    public static MyMultiImageSelector create(){
        if(sSelector == null){
            sSelector = new MyMultiImageSelector();
        }
        return sSelector;
    }

    public MyMultiImageSelector showCamera(boolean show){
        mShowCamera = show;
        return sSelector;
    }

    public MyMultiImageSelector count(int count){
        mMaxCount = count;
        return sSelector;
    }

    public MyMultiImageSelector single(){
        mMode = MultiImageSelectorActivity.MODE_SINGLE;
        return sSelector;
    }

    public MyMultiImageSelector multi(){
        mMode = MultiImageSelectorActivity.MODE_MULTI;
        return sSelector;
    }

    public  MyMultiImageSelector corpHead() {
        mMode = MultiImageSelectorActivity.MODE_CROP_HEAD;
        return sSelector;
    }

    public MyMultiImageSelector origin(ArrayList<String> images){
        mOriginData = images;
        return sSelector;
    }

    /**
     * 跳转和回调 ，requsetcode 使用的是 callback 的hashcode
     *
     */
    public void start(Activity activity, AvoidOnResult.Callback callback){
        final Context context = activity;
        if(hasPermission(context)) {
                //这里采用封装的那个
            new AvoidOnResult(activity).startForResult(createIntent(context),callback);

            }else{
            Toast.makeText(context, R.string.mis_error_no_permission, Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * 跳转和回调 ，requsetcode 使用的是 callback 的hashcode
     *
     */
    public void start(Fragment fragment, AvoidOnResult.Callback callback){
        final Context context = fragment.getContext();
        if(hasPermission(context)) {
            //这里采用封装的那个
            new AvoidOnResult(fragment).startForResult(createIntent(context),callback);

        }else{
            Toast.makeText(context, R.string.mis_error_no_permission, Toast.LENGTH_SHORT).show();
        }
    }

    private boolean hasPermission(Context context){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
            // Permission was added in API Level 16
            return ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED;
        }
        return true;
    }

    private Intent createIntent(Context context){
        Intent intent = new Intent(context, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, mShowCamera);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, mMaxCount);
        if(mOriginData != null){
            intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mOriginData);
        }
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, mMode);
        return intent;
    }
}
